# coding=utf-8
import input_data
import tensorflow as tf

#导入数据
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder("float", [None, 784])
#784维度 10维度
W = tf.Variable(tf.zeros([784,10]))
#偏差
b = tf.Variable(tf.zeros([10]))
#套用公式计算出概率
y = tf.nn.softmax(tf.matmul(x,W) + b)

#给一个新的占位符，为了输入的正确性
y_ = tf.placeholder("float", [None,10])

#交叉熵
cross_entropy = -tf.reduce_sum(y_*tf.log(y))

#反向传播算法，梯度下降优化
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
#开始训练
sess.run(init)

#训练1000次
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

#评估性能
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
#计算实际值
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
#计算正确率
print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
